<?php
class smart_phone_model extends MY_Model {
	private $module = 'smart_phone';
	private $table = 'contest';

	function getsearchContent($limit, $page) {
        $this->db->select('cli_contest.*,cli_user.username,cli_weeks.name as weeks_name');
        $this->db->from(PREFIX.'contest');
		$this->db->join(PREFIX.'user','cli_user.id = cli_contest.user_id','left');
        $this->db->join(PREFIX.'weeks','cli_weeks.id = cli_contest.week_id','left');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$content= $this->input->post('content');
			$this->db->where('(`cli_user`.`username` LIKE "%' . $this->input->post('content') .  '%" OR `cli_weeks`.`name` LIKE "%' . $this->input->post('content') . '%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get();

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }

    function getTotalsearchContent() {
        $this->db->select('cli_contest.*,cli_user.username,cli_weeks.name as weeks_name');
        $this->db->from(PREFIX.'contest');
		$this->db->join(PREFIX.'user','cli_user.id = cli_contest.user_id','left');
        $this->db->join(PREFIX.'weeks','cli_weeks.id = cli_contest.week_id','left');

       	if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$content= $this->input->post('content');
			$this->db->where('(`cli_user`.`username` LIKE "%' . $this->input->post('content') .  '%" OR `cli_contest`.`title` LIKE "%' . $this->input->post('content') . '%")');
		}
        if ($this->input->post('dateFrom') != '' && $this->input->post('dateTo') == '') {
            $this->db->where(PREFIX . $this->table . '.created >= "' . date('Y-m-d 00:00:00', strtotime($this->input->post('dateFrom'))) . '"');
        }
        if ($this->input->post('dateFrom') == '' && $this->input->post('dateTo') != '') {
            $this->db->where(PREFIX . $this->table . '.created <= "' . date('Y-m-d 23:59:59', strtotime($this->input->post('dateTo'))) . '"');
        }
        if ($this->input->post('dateFrom') != '' && $this->input->post('dateTo') != '') {
            $this->db->where(PREFIX . $this->table . '.created >= "' . date('Y-m-d 00:00:00', strtotime($this->input->post('dateFrom'))) . '"');
            $this->db->where(PREFIX . $this->table . '.created <= "' . date('Y-m-d 23:59:59', strtotime($this->input->post('dateTo'))) . '"');
        }
        $query = $this->db->count_all_results();

        if ($query > 0) {
            return $query;
        } else {
            return false;
        }
    }
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function saveManagement($fileName='',$flashName = ''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}
         if($this->input->post('is_winnerAdmincp')=='on'){
			$is_winner = 1;
		}else{
			$is_winner = 0;
		}
		if($this->input->post('hiddenIdAdmincp')==0){
			
			$data = array(
				'title'=> $this->input->post('titleAdmincp'),
				'content'=> $this->input->post('contentAdmincp'),
                'type'=> $this->input->post('type'),
				'file'=> $fileName['image'],
                'video'=> $fileName['video'],
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			
			//Xử lý xóa hình khi update thay đổi hình
			if($fileName['image']==''){
				$fileName['image'] = $result[0]->file;
			}else{
				
                $type = $this->input->post('type');
                            if($type=='image')
                            {
                                @unlink(BASEFOLDER.DIR_UPLOAD_IMAGE.$result[0]->file);
                            }
                            if($type=='baicamnhan')
                            {
                                @unlink(BASEFOLDER.DIR_UPLOAD_BAIVIET.$result[0]->file);
                            }
                            
			}
            if($fileName['video']==''){
				$fileName['video'] = $result[0]->video;
			}else{
				
                 @unlink(BASEFOLDER.DIR_UPLOAD_VIDEO.$result[0]->video);
                            
			}
			//End xử lý xóa hình khi update thay đổi hình
			
			$data = array(
                'title'=> $this->input->post('titleAdmincp'),
				'content'=> $this->input->post('contentAdmincp'),
                'type'=> $this->input->post('type'),
				'file'=> $fileName['image'],
                'video'=> $fileName['video'],
				'status'=> $status,
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}
    
    function get_profile($fb_id) {
        $this->db->select('cli_user.*');
        $this->db->where('cli_user.status', 1);
        $this->db->where('cli_user.fb_id', $fb_id);
        $this->db->from(PREFIX.'user');
       	$query = $this->db->get();
        if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    function check_code($code)
    {
        $this->db->select('cli_contest.*');
        $this->db->where('cli_contest.code', $code);
        $this->db->where('cli_contest.status', 1);
        $this->db->from(PREFIX.'contest');
       	$query = $this->db->get();
        if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    function get_weeks()
    {
        $this->db->select('*');
        $this->db->where('active',1);
        $this->db->where('status',1);
        $this->db->limit(1);
        $query = $this->db->get('cli_weeks');
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    function winner_weeks()
    {
        $this->db->select('cli_contest.week_id');
        $this->db->from('cli_contest');
        $this->db->where('cli_contest.status',1);
        $this->db->group_by('week_id');
	    $query = $this->db->get();
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
   	function detail_weeks($id)
    {
        $this->db->select('*');
        $this->db->where('status',1);
        $this->db->where('id',$id);
        $query = $this->db->get('cli_weeks');
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    
    function list_winner_weeks($week_id)
    {
        $this->db->select('cli_contest.*,cli_user.fullname ,cli_user.email');
        $this->db->from('cli_contest');
        $this->db->join('cli_user', 'cli_user.id = cli_contest.user_id',"left");
        $this->db->limit(30);
        $this->db->where('cli_contest.status',1);
        $this->db->where('cli_contest.is_winner',1);
        $this->db->where('cli_contest.week_id',$week_id);
	    $query = $this->db->get();
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
    function get_prices()
    {
        $this->db->select('cli_prices.*');
        $this->db->from('cli_prices');
        $this->db->where('cli_prices.status',1);
	    $query = $this->db->get();
  		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
    }
	
}